// CommandLineHelper Library
// =========================
// License: Microsoft Permissive License (Ms-PL) v1.1
// Author: Rhy Mednick (http://www.rhyverse.net)
// Project Site: http://www.codeplex.com/CommandLineHelper
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using CommandLineHelper.Attributes;

namespace CommandLineHelper.Tests
{
    [TestFixture]
    public class ValidateRangeAttribute_Test
    {
        /// <summary>
        ///A test for IsValid (object)
        ///</summary>
        [Test]
        public void IsValid_ValidateRangeAttribute()
        {
            ValidateRangeAttribute target = new ValidateRangeAttribute();
            target.MinRange = -1024;
            target.MaxRange = 1024;

            //test outside range (low)
            object value = long.MinValue;
            bool expected = false;
            bool actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateRangeAttribute.IsValid did not return the expected value.");

            //test outside range (high)
            value = long.MaxValue;
            expected = false;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateRangeAttribute.IsValid did not return the expected value.");

            //test both boundary conditions
            value = target.MinRange;
            expected = true;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateRangeAttribute.IsValid did not return the expected value.");

            value = target.MaxRange;
            expected = true;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateRangeAttribute.IsValid did not return the expected value.");

            //test middle of range condition
            value = 0;
            expected = true;
            actual = target.IsValid(value);
            Assert.AreEqual(expected, actual, "CommandLineHelper.Attributes.ValidateRangeAttribute.IsValid did not return the expected value.");
        }

        /// <summary>
        ///Verify that the min/max range properties can be set and read properly
        ///</summary>
        [Test]
        public void MinMaxRangeTest()
        {
            ValidateRangeAttribute target = new ValidateRangeAttribute();

            //min and max can be negative
            long minVal = -100;
            try
            {
                target.MinRange = minVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(minVal, target.MinRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MinRange was not set correctly.");
            }
            long maxVal = -10;
            try
            {
                target.MaxRange = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(maxVal, target.MaxRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MaxRange was not set correctly.");
            }

            target = new ValidateRangeAttribute(); //reset
            //min and max can both be zero independently
            minVal = 0;
            maxVal = 0;
            try
            {
                target.MinRange = minVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(minVal, target.MinRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MinRange was not set correctly.");
            }
            target = new ValidateRangeAttribute(); //reset
            try
            {
                target.MaxRange = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(maxVal, target.MaxRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MaxRange was not set correctly.");
            }
            //min and max cannot be the same (max is alredy zero, so just set the min and hope it breaks)
            try
            {
                target.MinRange = minVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(minVal, target.MinRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MinRange was not set correctly.");
            }


            target = new ValidateRangeAttribute(); //reset
            //min cannot be greater than max, set min first
            minVal = 100;
            maxVal = 50;
            try
            {
                target.MinRange = minVal;
                target.MaxRange = maxVal;
            }
            catch { }
            finally
            {
                Assert.AreEqual(minVal, target.MinRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MinRange was not set correctly.");
                Assert.AreNotEqual(maxVal, target.MaxRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MaxRange was not set correctly.");
            }

            target = new ValidateRangeAttribute(); //reset
            //min cannot be greater than max, set max first
            minVal = 100;
            maxVal = 50;
            try
            {
                target.MaxRange = maxVal;
                target.MinRange = minVal;
            }
            catch { }
            finally
            {
                Assert.AreNotEqual(minVal, target.MinRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MinRange was not set correctly.");
                Assert.AreEqual(maxVal, target.MaxRange, "CommandLineHelper.Attributes.ValidateRangeAttribute.MaxRange was not set correctly.");
            }

        }
    }
}
